﻿using System;
using System.Collections.Generic;
using System.Text;
using CadStrategy2013.DxfData;
using MyTool.FileOpt;

namespace CadStrategy2013.CgqStyle
{
    public class GdClass : Cgq2013
    {
        private string m_ceng;
        private int m_bNo;

        public GdClass(string secname)
            : base(secname)
        {
        }

        //public override void GetCfg()
        //{
        //    base.GetCfg();
        //    GetCfg("模入板");
        //}
        public override List<string> ReadTemplateDxf()
        {
            return ReadDxf(Path + "\\template\\模入板.dll");
        }

        public override string GetSaveFileName(int index)
        {
            if (ShiJiTuZhiCount == 1)
                return "\\轨道.dxf";
            return "\\轨道" + (index + 1).ToString() + ".dxf";
            //if (index == 0)
            //    return "\\轨道.dxf";
            //else
            //    return "\\轨道" + index + ".dxf";
        }

        public override DxfDataClass GetDxfData()
        {
            DxfDataClass dxfDataClass = new DxfDataClass();
            IniFile inifile = new IniFile("./\\" + StationName + "\\gd.txt");
            inifile.Load();
            string strtemp = inifile.GetString("轨道", "板号");
            try
            {
                int l = strtemp.IndexOf('-');
                m_ceng = strtemp.Substring(0, l);
                int.TryParse(strtemp.Substring(l + 1, strtemp.Length - l - 1), out m_bNo);
            }
            catch
            {
                m_ceng = "";
                m_bNo = 0;
            }
            int sum0 = inifile.GetInt("轨道", "sum", 0);

            for (int i = 0; i < sum0; i++)
            {
                DevClass devClass = new DevClass();
                string strs = inifile.GetString("轨道", (i + 1).ToString());
                string[] s = strs.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
                int n = s.Length;
                if (n == 0)
                {
                    devClass.DevName = " ";
                }
                else
                {
                    devClass.DevName = s[0];
                }
                if (n < 2)
                {
                    devClass.peiList.Add("");
                    devClass.peiList.Add("");
                }
                else
                {
                    devClass.peiList.Add(s[1]);
                    devClass.peiList.Add(s[2]);
                }
                dxfDataClass.devList.Add(devClass);
            }

            if (sum0 != 0)
            {
                ShiJiBanCount = (dxfDataClass.devList.Count - 1) / BanDevCount + 1;
                ShiJiTuZhiCount = (ShiJiBanCount - 1) / BanCount + 1;
            }
            else
            {
                ShiJiBanCount = 0;
                ShiJiTuZhiCount = 0;
            }
            m_dxfDataClass = dxfDataClass;
            return dxfDataClass;
        }

        public override List<string> DealData(List<string> strList, int fi)
        {
            List<string> list = new List<string>();
            int n = 0;
            foreach (string str in strList)
            {
                string str1 = str;
                str1 = ReplaceStationName(str1);
                str1 = ReplacebNo(str1, fi);
                str1 = ReplaceDev(str1, fi);
                //str1 = ReplaceCPei(str1, i);
                str1 = ReplacePei(str1, fi);
                list.Add(str1);
                OnDealData(this, new WriteDxfArgs(SecName + fi.ToString(), str, strList.Count, ++n));
            }
            return list;
        }

        public override string ReplaceDev(string str2, int fi = 0)
        {
            string str = str2;
            for (int j = 1; j <= BanCount; j++)
            {
                int nj = j;// -fi * BanCount;
                for (int i = 1; i <= BanDevCount; i++)
                {
                    string str1 = "dev" + nj.ToString() + "-" + i.ToString() + ">";
                    if (str.Contains(str1))
                    {
                        int index = (fi * BanCount + (j - 1)) * BanDevCount + i - 1;
                        //int index = (j - 1) + fi * BanCount;
                        if (index >= m_dxfDataClass.devList.Count)
                            return "";
                        try
                        {
                            return str = str.Replace(str1, m_dxfDataClass.devList[index].DevName);
                        }
                        catch
                        {
                            return "";
                        }
                    }
                }
            }
            return str;
        }

        public override string ReplacePei(string str, int fi = 0)
        {
            if (fi == 1)
            {
            }
            for (int j = 1; j <= BanCount; j++)
            {
                int nj = j;// -f * BanCount;
                for (int i = 1; i <= 2 * BanDevPeiCount * BanDevCount; i++)
                {
                    string str1 = "pei" + nj.ToString() + "-" + i.ToString() + ">";
                    if (str.Contains(str1))
                    {
                        int index = (fi * BanCount + (j - 1)) * BanDevCount + (i - 1) / BanDevPeiCount;
                        if (index >= m_dxfDataClass.devList.Count)
                            return "";
                        int index1 = (i - 1) % 2;
                        try
                        {
                            return str = str.Replace(str1, m_dxfDataClass.devList[index].peiList[index1]);
                        }
                        catch
                        {
                            return "";
                        }
                    }
                }
            }
            return str;
        }

        public override string ReplacebNo(string str, int fi = 0)
        {
            //return base.ReplacebNo(str, fi);
            string str1 = str;

            string str2 = "CX-Y";
            if (str.Contains(str2))
            {
                if (ShiJiBanCount == 0)
                {
                    string s = str;
                    return s.Replace(str2, "");
                }
                return str.Replace(str2, GetCengNo(0, fi));
            }

            return str1;
        }

        private string GetCengNo(int n, int fi)
        {
            int cengNo;
            int.TryParse(m_ceng.Substring(1, m_ceng.Length - 1), out cengNo);
            int m = n + fi;// *BanCount;
            //if (m > ShiJiBanCount)
            //    return "";
            int n1 = m + m_bNo;
            if (m_ceng == "C1")
            {
                if (n1 >= 8)
                {
                    int cn = (n1 - 8) / 8 + 2;
                    int ws = (n1 - 8) % 8 + 2;
                    return "C" + cn + "-" + ws;
                }
                else
                    return "C1-" + n1;
            }
            else
            {
                int cn = (n1 - 2) / 8 + cengNo;
                int ws = (n1 - 2) % 8 + 2;
                return "C" + cn + "-" + ws;
            }
        }
    }
}